package com.sample.systemui.view;

import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.lifecycle.Observer;

import com.sample.servicefragment.DummyServiceFragment;
import com.sample.systemui.R;
import com.sample.systemui.viewmodel.StatusBarLiveData;
import com.sample.systemui.viewmodel.StatusBarViewModel;
import com.sample.servicefragment.ViewModelProviders;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class StatusBarServiceFragment extends DummyServiceFragment {

    private Unbinder mUnbinder = null;

    private StatusBarViewModel mStatusBarViewModel = null;

    /**
     * Sample6:step6-1
     * 实现 setOnTouchListener 方法
     */
    private View.OnTouchListener mOnTouchListener = null;
    public void setOnTouchListener(View.OnTouchListener onTouchListener) {
        this.mOnTouchListener = onTouchListener;
        View view = getView();
        if(null != view) {
            view.setOnTouchListener(mOnTouchListener);
        }
    }

    @BindView(R.id.text_datetime)
    TextView mTextDatetime;

    //todo 如果需要监听View事件，可以在这里为各个控件设定监听OnClick事件
    @OnClick(R.id.text_datetime)
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.text_datetime:
                Log.d(getContext().getClass().getCanonicalName(), "text_datetime clicked.");
                break;
            default:
                break;
        }
    }

    public StatusBarServiceFragment() {
        super();
        // 不要在这里绑定ViewModel，设定LiveData观察者
        // 因为LiveData中需要用到本实例对象，而现在对象还没有创建完毕。
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // todo 可以在这里绑定ViewModel，设定LiveData观察者
        mStatusBarViewModel = ViewModelProviders.of(this)
                .get(StatusBarViewModel.class);
        mStatusBarViewModel.getViewLiveData().observe(this,
                new Observer<StatusBarLiveData<Integer, String>>() {
                    @Override
                    public void onChanged(StatusBarLiveData<Integer, String> data) {
//                        Log.d()
                    }

                });
        mStatusBarViewModel.getViewLiveData().observe(this,
                new Observer<Integer>() {
                    @Override
                    public void onChanged(Integer data) {
//                        Log.d()
                    }

                });

        /**
         * sample005: ViewMutableLiveData 使用例子
         * 3.在 View 层为 ViewMutableLiveData 变量绑定观察者。
         */
        mStatusBarViewModel.getBluetoothViewLiveData().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                // TODO 根据参数变化，更新界面.

            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater) {
        // notice:因为不是真正的fragment，不是某个Activity的一部分，所以没有父控件，root应该设置为null，
        // attachToRoot 应该设置为false，
        View view = inflater.inflate(R.layout.layout_status_bar, null, false);
//        View view = View.inflate(getContext(), R.layout.layout_status_bar, null);
        /**
         * Sample6:step6-2
         * 在 onCreateView 中为 view 绑定 OnTouchListener.
         */
        if(null != mOnTouchListener) {
            view.setOnTouchListener(mOnTouchListener);
        }
        mUnbinder = ButterKnife.bind(this, view); // 用于支持ButterKnife

        //todo 如果需要监听View事件，也可以在这里为各个控件设定监听Listener

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(null != mUnbinder) {
            mUnbinder.unbind();
        }
    }

    @Override
    public WindowManager.LayoutParams createLayoutParams() {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                /* width */
                WindowManager.LayoutParams.MATCH_PARENT,
                /* height */
                WindowManager.LayoutParams.WRAP_CONTENT,
                /* type */
//                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.TYPE_STATUS_BAR,
                /* flag */
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING/*
                | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH*/
                | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                /* format */
                PixelFormat.RGBA_8888
        );
        layoutParams.gravity = Gravity.TOP | Gravity.CENTER;
        layoutParams.setTitle("StatusBar");
        layoutParams.packageName = getContext().getPackageName();
        return layoutParams;
    }
}
